(function () {
    var keyContactTable;//关键任务信息表格
    var keyContactData={};
    var contractTable;///客户合同表格
	var contractData = {};
	var afterSaleTable;///售后表格
	var afterSaleData = {};
	var serverData = {};//机会表格
	var	serverTable;
	var serverDataBid = {};//投标
	var serverTableBid;
	var serverDataVis = {};//来访
    var serverTableVis;
    var serverDataPrice={};//价格单
    var serverTablePrice;
    //默认加载列表
    jQuery(document).ready(function () {
    	/**
    	 * 查看关键人物信息
    	 */
    	keyContactData.customerId=jQuery('#customerId').val();
    	keyContactTable = jQuery('#keyTable').DataTable({
            "serverSide": true,
            "iDisplayLength":5,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/customer/getKeyContact',
                "data": function (d) {
                    return $.extend({}, d, keyContactData);
                }
            },
            columns: [
                {
                    data: null,
                    defaultContent: ""//排序
                },
                /*{
                    data: null,
                    defaultContent: "",
                    render: function (data, type, full, meta) {
    	            	var id=data.id
    	            	var operate ='<input type="checkbox" class="checkchild">';
    	            	return operate;
    	            }   
                },*/
                {
                    data: 'name'
                },
                {
                    data: 'calledName'
                },
                {
                    data: 'contractType'
                },	             
                {
                    data: 'position'
                },
                {
                    data: 'major'
                },
                {
                    data: 'domain'
                },
                {
                    data: 'memo'
                }
                ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    	/**
    	 * 查看合同相关信息
    	 */
    	//if(contractTable!=null) return;//如果contractTable存在直接返回
		var customerId=jQuery('#customerId').val();
		contractData.customerId=customerId;
		contractData.key='';
    	contractTable= jQuery('#contractTable').DataTable({
            "serverSide": true,
            "iDisplayLength":5,
            "ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
                "url":window.rootPath+ '/ui/contract/getCustomerContract',
                "data": function (d) {
                    return $.extend({}, d, contractData);
                }
            },
            columns: [
	                {
	                	data: null
	                },
	                {
	                    data: 'supplierCode'
	                },
	                {
	                	data: 'contractType'
	                },
	                /*{
	                	 data: 'auditState',
	                     render: function (data, type, full, meta) {
	                      	var dOut;
	                      	 if(data=='审批中'){
	                      		dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
	                      	}
	                      	else if(data=='驳回'){
	                      		dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
	                      	}
	                      	else if(data=='通过'){
	                      		dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
	                      	}
	                      	else  if(data=='未提交'){
	                      		dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
	                      	}
	                     	else  if(data=='已提交'){
	                      		dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
	                      	}
	      	            	return dOut;
	                      }
	                },*/
	                {
	                    data: 'belongArea'
	                },
	                {
	                    data: 'totalSum'
	                },
	                {
	                    data: 'scheduleDate',
	                    render: function (data, type, full, meta) {
	                        if (data)
	                            return moment(data).format('YYYY/MM/DD');
	                        else
	                            return '';
	                    }
	                },
	                {
	                    data: 'deliveryDate',
	                    render: function (data, type, full, meta) {
	                        if (data)
	                            return moment(data).format('YYYY/MM/DD');
	                        else
	                            return '';
	                    }
	                },
	                {
	                    data: 'stop'
	                },
	                {
	                    data: 'freightCharge'
	                },
	                {
	                    data: 'projectName'
	                },
	                {
	                    data: 'orderUnit'
	                },
	                {
	                    data: 'orderAddress'
	                },
	                {
	                    data: 'orderDelegate'
	                },
	                {
	                    data: 'orderPhone'
	                },
	                {
	                    data: 'orderZip'
	                },
	                {
	                    data: 'orderArea'
	                },
	                {
	                    data: 'postUnit'
	                },
	                {
	                    data: 'postAddress'
	                },
	                {
	                    data: 'postDelegate'
	                },
	                {
	                    data: 'postPhone'
	                },
	                {
	                    data: 'postZip'
	                }
                ],
            "drawCallback": function (oSettings) {
                var k = 0;
                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                    k++;
                }
            }
        });
    	/**
    	 * 售后维护表格
    	 */
    	afterSaleData.key="";
		afterSaleData.customerId=jQuery("#customerId").val();
		afterSaleTable=jQuery("#customerserviceTable").DataTable({
			"serverSide":true,
			"iDisplayLength":5,
			"ordering": false,
            "ajax": {
                "dataType": "json",
                "type": "get",
                "cache": false,
				"url":window.rootPath+"/ui/afterSaleTicket/getAfterSalepage",
				"data":function(d){
					return $.extend({},d,afterSaleData);
				}
			},
			columns:[
			    {
			    	data:null
			    },
			    {
                	data: null,
                	 render: function (data, type, full, meta) {
	                        var dOut;
	                        if(data.isSubmit=="0"){
	                            dOut = '<td class="center" ><span class="badge label-pill label-danger">未提交</span></td>';
	                        }     
	                        else if(data.isSubmit=="1") {
	                            dOut = '<td class="center" ><span class="badge label-pill label-danger">退回</span></td>';
	                        }   
	                        else if(data.isSubmit=="2") {
	                            dOut = '<td class="center" ><span class="badge label-pill label-primary">已提交</span></td>';
	                        } 
	                        else if(data.isSubmit=="3") {
	                            dOut = '<td class="center" ><span class="badge label-pill label-primary">已派遣</span></td>';
	                        } 
	                        else if(data.isSubmit=="4") {
	                            dOut = '<td class="center" ><span class="badge label-pill label-primary">跟踪中</span></td>';
	                        } 
	                        else if(data.isSubmit=="5") {
	                            dOut = '<td class="center" ><span class="badge label-pill label-primary">待回访</span></td>';
	                        } 
	                        else if(data.isSubmit=="6") {
	                            dOut = '<td class="center" ><span class="badge label-pill label-success">已完成</span></td>';
	                        }
	                        return dOut;
	                    }
                },
                {
            	    data: null,
            	    render : function(data,type, full,meta) {
						var dOut;
						if (data.ticketState == '审批中') {
							dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
						} else if (data.ticketState == '驳回') {
							dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
						} else if (data.ticketState == '通过') {
							dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
						} else if (data.ticketState == '未提交') {
							dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
						} else if (data.ticketState == '已提交') {
							dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
						}else if(data.ticketState==null){
							dOut='';
						}
						return dOut;
					}
                },
                {
                	data:'afterSaleType'
                },
                
                {
                	data:'deviceName'
                },
                {
                	data:'deviceNumber'
                },
                {
                	data: 'questionDetail'
                },
                {
                	data:'serviceTime',
                	render: function (data, type, full, meta) {
                            if (data)
                                return moment(data).format('YYYY/MM/DD');
                            else
                                return '';
                     }
                },
                {
                    data: 'contractCode'
                },
                {
                	data:'proposer'
                },
                {
                	data:'superior'
                },
                {
                	data:'createTime',
                	render: function (data, type, full, meta) {
                        if (data)
                            return moment(data).format('YYYY/MM/DD');
                        else
                            return '';
                   }
                },
                {
                	data:'submiter.empLogin.userName'
                },
                {
                    data: 'manageCode'
                },
                
                {
                    data: 'unitName'
                },
                
                
                {
                	data:'linkMan'
                },
                {
                	data:'linkManPhone'
                },
                
                {
                	data:'deviceCode'
                }
			 ],
			 "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	            }
		});
		/**
		 * 机会表格
		 */
		 serverData.key =jQuery("#customerId").val();
		 serverTable=  jQuery('#customerIdTable').DataTable({
			 "serverSide": true,
	            "iDisplayLength":5,
	            "ordering": false,
		        "ajax": {
		            "dataType": "json",
		            "type": "get",
		            "cache": false,
		            "url": window.rootPath+'/ui/chance/getpage',
		            "data": function (d) {
		                return $.extend({}, d, serverData);
		            }
		        },
		        columns: [
		            {
		            data: null,
		            defaultContent: ""
		            },/*{
				    data:null,
				    render: function(data, type, full, meta){
				    	var operate ='<input type="checkbox" class="checkchild">';
				    	var id=data.id;
				    	return operate;
								
						}
		           },*/
		            {
		            	 data: null,
						 render: function(data, type, full, meta){
							var dOut;
		                    var isSubmit=data.isSubmit;
		                    var isSucceed = data.isSucceed;
		                    if(isSucceed != null){
		                		//dOut ='<p><span class="badge" style="background-color:red;font-family:arial;color:bule">已关闭-'+data.isSucceed+'</span></p>';
		                		dOut ='<td class="center" ><span class="badge label-pill label-danger ">已关闭-'+data.isSucceed+'</span></td>';
		                	}else{
		                    if(isSubmit == "1")
		                   {
		                    	dOut ='<td class="center" ><span class="badge label-pill label-success style="background-color:green;">已提交</span></td>';
		                    	//dOut = '<p><span class="badge" style="background-color:green;font-family:arial;color:bule">已提交</span></p>';
		                   }
		                    else{
		                    	//dOut = '<p><span class="badge">未提交</span></p>';
		                    	dOut ='<td class="center" ><span class="badge label-pill label-primary">未提交</span></td>';
		                    }
							}
		                    return dOut;
						 }
		            },
		            {
		            	data: 'name'
		            },
		            {
		                data: 'user.empLogin.userName'
		            },
		            {
		                data: 'user.saleArea.superiorArea'
		            },
		            {
		                data: 'sourcePower'
		            },
		            {
		                data: 'orderAmount'
		            },
		            {
		                data: 'orderUnit'
		            },
		            {
		                data: 'orderPerson'
		            },
		            {
		            	data: 'chancePriority'
		            },
		            
		            {
		            	data: 'business'
		            },
		            {
		                data: 'source'
		            },
		            {
		                data: 'sourceUnit'
		            },
		            
		            {
		                data: 'sourceOfferInfo'
		            },
		            {
		                data: 'projectState'
		            },
		            {
		                data: 'deviceState'
		            },
		            
		            /*{
		                data: 'orderUnitAddr'
		            },*/
		            {
		            	data: 'createTime',
		            	render : function(data,type, full,meta) {
								if (data) 
								{
									return moment(data).format("YYYY-MM-DD");
								}
		            	}
		            },
		            {
		            	data: 'memo'
		            }
		            ],
		            "drawCallback" : function(oSettings) {
						var k = 0;
						var total = oSettings.aiDisplay.length
								+ oSettings._iDisplayStart;
						for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
							$(
									'td:eq(0)',
									oSettings.aoData[oSettings.aiDisplay[k]].nTr)
									.text(i + 1);
							k++;
						}
					}
		    });
		/**
		 * 投标
		 */
		 serverDataBid.key =jQuery("#customerId").val();
		 serverTableBid = jQuery('#toubiao').DataTable(
					{
						"serverSide" : true,
						"ordering" : false,
						"ajax" : {
							"dataType" : "json",
							"type" : "get",
							"cache" : false,
							"url" : window.rootPath+ '/ui/bid/getpage',
							"data" : function(d) {
								return $.extend({}, d,
										serverDataBid);
							}
						},
						columns : [
								{
									data : null
								},
								/*{
									data : null,
									defaultContent : "",
									render : function(data,
											type, full,
											meta) {
										var id = data.id
										var operate = '<input type="checkbox" class="checkchild">';
										return operate;
									}
								},*/
								{
									data : 'name'
								},
								{
									data : 'type'
								},
								{
									data : 'cash'
								},
								{
									data : 'bidDate',
										render: function (data, type, full, meta) {
				                            if (data)
				                                return moment(data).format('YYYY/MM/DD');
				                            else
				                                return '';
				                     }
								},
								{
									data : 'costSum',
								},
								{
									data : 'sum',
								},
								{
									data : 'projectType'
								},
								{
									data : 'launcher'
								},
								{
									data : 'launch'
								},
								{
									data : 'ownerUnit'
								},
								{
									data : 'bidUnit'
								},
								{
									data : 'auditState',
									render : function(data,
											type, full,
											meta) {
										var dOut;
										if (data == '审批中') {
											dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
										} else if (data == '驳回') {
											dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
										} else if (data == '通过') {
											dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
										} else if (data == '未提交') {
											dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
										} else if (data == '已提交') {
											dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
										}
										return dOut;
									}
								} 
							],
						"drawCallback" : function(oSettings) {
							var k = 0;
							var total = oSettings.aiDisplay.length
									+ oSettings._iDisplayStart;
							for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
								$(
										'td:eq(0)',
										oSettings.aoData[oSettings.aiDisplay[k]].nTr)
										.text(i + 1);
								k++;
							}
						}
					});
		 /**
		  * 来访
		  */
		 serverDataVis.key =jQuery("#customerId").val();
		 serverTableVis = jQuery('#visitTable').DataTable({
			 "serverSide": true,
	            "iDisplayLength":5,
	            "ordering": false,
	            "ajax": {
	                "dataType": "json",
	                "type": "get",
	                "cache": false,
	                "url":window.rootPath+ '/ui/customerVisiting/getpage',
	                "data": function (d) {
	                    return $.extend({}, d, serverDataVis);
	                }
	            },
	            columns: [
	                {
	                	data: null
	                },
	                /*{
	                    data: null,
	                    defaultContent: "",
	                    render: function (data, type, full, meta) {
	                    	var id=data.id
	    	            	var operate ='<input type="checkbox" class="checkchild">';
	    	            	return operate;

	                    }
	                },
	                {
	                    data: 'customer.customerCode'
	                },
	                {
	                    data: 'customer.customerName'
	                },*/
	                {
	                    data: 'visitingDate',
	                    render: function (data, type, full, meta) {
	                            if (data)
	                                return moment(data).format('YYYY/MM/DD');
	                            else
	                                return '';
	                     }
	                },
	                {
	                    data: 'project'
	                },
	                {
	                	data: 'contractAmount'
	                },
	                {
	                	data: 'development'
	                },
	                {
	                	data: 'customer.creditLevel'
	                },
	                {
	                	data: 'operator'
	                },
	                {
	                	data: 'prove'
	                },
	                /*{
	                	data: 'saleArea.superiorArea'
	                },*/
	                {
	                	data: 'memo'
	                }
	                ],
	            "drawCallback": function (oSettings) {
	                var k = 0;
	                var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

	                for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                    $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                    k++;
	                }
	            }
	        });
		 /**
		  * 价格单
		  */
		 serverDataPrice.key =jQuery("#customerId").val();
		 serverTablePrice = jQuery('#dataTablePrice').DataTable({
			 "serverSide": true,
            "iDisplayLength":5,
            "ordering": false,
	         "ajax": {
	             "dataType": "json",
	             "type": "get",
	             "cache": false,
	             "url":window.rootPath+ '/ui/CustomerTotalIndex/price',
	             "data": function (d) {
	                 return $.extend({}, d, serverDataPrice);
	             }
	         },
	         columns: [
	             {
	             	data: null,
	             	defaultContent: ""
	             },
	             /*{
	                 data: null,
	                 defaultContent: "",
	                 render: function (data, type, full, meta) {
	                 	var id=data.id
	 	            	var operate ='<input type="checkbox" class="checkchild">';
	 	            	return operate;
	                 }
	             },*/
	             {
	                 data: 'name'
	             },
	             {
	            	 data:'customerName'
	             },
	             {
		             	data: 'deliveryDate',
		             		render : function(data,type, full,meta) {
		 						if (data) 
		 						{
		 							return moment(data).format("YYYY-MM-DD");
		 						}
		             	}
		          },
	             {
	            	 data: 'auditState',
	                 render: function (data, type, full, meta) {
	                  	var dOut;
	                  	 if(data=='审批中'){
	                  		dOut = '<td class="center" ><span class="badge label-pill label-primary">审批中</span></td>';
	                  	}
	                  	else if(data=='驳回'){
	                  		dOut = '<td class="center" ><span class="badge label-pill label-danger">驳回</span></td>';
	                  	}
	                  	else if(data=='通过'){
	                  		dOut = '<td class="center" ><span class="badge label-pill label-success">通过</span></td>';
	                  	}
	                  	else  if(data=='未提交'){
	                  		dOut = '<td class="center" ><span class="badge label-pill label-default">未提交</span></td>';
	                  	}
	                 	else  if(data=='已提交'){
	                  		dOut = '<td class="center" ><span class="badge label-pill label-warning">已提交</span></td>';
	                  	}
	  	            	return dOut;
	                  }
	             },
	             /*{
	                 data: 'sum'
	             },*/
	             {
	                 data: 'costSum'
	             },
	             {
	                 data: 'sum'
	             },
	             {
	             	data: 'createTime',
	             		render : function(data,type, full,meta) {
	 						if (data) 
	 						{
	 							return moment(data).format("YYYY-MM-DD");
	 						}
	             	}
	             },
	             {
	             	data: 'memo'
	             },
	             {
	            	 data:"type",
	             }
	             
	             ],
	         "drawCallback": function (oSettings) {
	             var k = 0;
	             var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;
	             for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
	                 $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
	                 k++;
	             }

	         }
	     });
	     
    });
})();





